"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:PersonnelReturnycsqCtrl
 * @description
 * # PersonnelReturnycsqCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("PersonnelReturnycsqCtrl", function (
    $scope,
    $routeParams,
    TaskReviewService,
    DataService,
    Util,
    openApi
  ) {
    // 详情接口，审批记录接口，提交审核接口
    var id = ($scope.process_id = $routeParams.id);
    $scope.taskId = id;
    var detailDataUrl = openApi.getTaskInfo;
    // 获取回显的详细信息
    var getDetailData = function (callback) {
      var options = {};
      options.param = {};
      options.param.processInstanceId = id;
      options.param.taskId = id;
      options.url = detailDataUrl;
      options.callback = callback;
      var TaskReview = new TaskReviewService(options);
      $scope.TaskReview = TaskReview;
    };
    // 提交
    $scope.check = function (item) {
      var check = $scope.validate();
      if (check) {
        navigator.notification.confirm(
          "是否确定执行该操作",
          $scope.submit.bind($scope, item.id),
          "提示",
          ["确定", "取消"]
        );
      }
    };

    $scope.checkmore = function () {
      $scope.hidedisagree = !$scope.hidedisagree
    };

    $scope.showAuditContent = function (item, $event) {
      if (item.id === 20) {
        // 如果是转交则执行这里的操作
        Util.showFlowTransfer({
          scope: $scope
        })
        $rootScope.$emit("showmasker");
        return
      }
      if (item.id === 30) {
        // 回退的操作
        Util.showFallbackFlow({
          scope: $scope,
        });
        $rootScope.$emit("showmasker");
        return;
      }
      $scope.hidedisagree = true
      $event && $event.stopPropagation()        // 设置蛇皮操作，当页面跳转时候保存起来
      $scope.check(item)
    };
    $scope.submit = function (operationId) {
      // 底部类型end
      var param = {};
      // 根据operationId判断是草稿还是提交
      param = angular.copy($scope.TaskReview.result, {});
      param.operationId = operationId;
      param.attachmentName = $scope.attachmentName || "";
      DataService.getData(openApi.returnCar, param).then(function (data) {
        Util.clearTVType();
        Util.clearFormData();
        Util.Toast(data, {
          callback: function () {
            Util.goBack();
            NativeUtil.use("close");
            $scope.$apply();
          },
          time: 1000
        });
        // 提交成功后刷新列表
        NativeUtil.use("updateProcess");
      });
    };
    $scope.validate = function () {
      if (!$scope.TaskReview.result.startUseDate) {
        Util.Toast("请选择用车起始日期");
        return;
      }
      if (!$scope.TaskReview.result.endUseDate) {
        Util.Toast("请选择用车结束日期");
        return;
      }
      if (!$scope.TaskReview.result.costTime) {
        Util.Toast("请填写用车时长");
        return;
      }
      if (!$scope.TaskReview.result.unit) {
        Util.Toast("请选择时间单位");
        return;
      }
      return true;
    };
    // 如果是审批中并且当前登录用户是当前发起人，可以显示提醒一下
    var init = function () {
      getDetailData(function (data, formKey) {
        $scope.formKey = formKey;
        $scope.showaudit = true;
        $scope.hidedisagree = true
        // 根据返回数据显示按钮
        Util.showAudit({
          scope: $scope
        });
      });
    };
    // 初始化
    init();
  });
